package sqlplus;
import java.sql.*;
import java.util.Scanner;
import java.io.*;

public class TestBlob_10g {// 对Oracle-10g版本
        public static void main(String[] args) {
                Scanner sca = new Scanner(System.in);
                System.out.println("input your filename:");
                String fileName = sca.next();

                Connection con = null;
                PreparedStatement ps = null;
                ResultSet rs = null;
                Blob blob = null;
                InputStream in = null;
                int id = 15;
                try {	
                        con = JdbcUtil.getConn();
                        con.setAutoCommit(false);
                        in = new FileInputStream(fileName);
                        byte[] content = new byte[in.available()];
                       
                        String sql = "insert into lotus values(?,?,?)";
                       
                        ps = con.prepareStatement(sql);
                        ps.setInt(1, id);
                        ps.setString(2, fileName);
                        ps.setBytes(3, content);
                        ps.executeUpdate();                    
                        JdbcUtil.close(null, ps, null);
                       
                        sql = "select FILECONTENT from lotus where ID=? for update";
                        ps = con.prepareStatement(sql);
                        ps.setInt(1, id);
                        rs = ps.executeQuery();
                        if (rs.next()) {
                                blob = rs.getBlob(1);
                        }
                        sql = "update lotus set FILECONTENT=? where ID=?";
                        JdbcUtil.close(rs, ps, null);
                        ps = con.prepareStatement(sql);
                        ps.setBlob(1, blob);
                        ps.setInt(2, id);
                        ps.executeUpdate();
                        con.commit();

                } catch (Exception e) {
                        e.printStackTrace();
                        try {
                                con.rollback();
                        } catch (Exception ex) {
                                ex.printStackTrace();
                        }
                } finally {
                        JdbcUtil.close(null, ps, con);
                        try {
                                in.close();
                        } catch (Exception ex) {
                                ex.printStackTrace();
                        }
                }
        }
}